const app = getApp();

 const baseUrl = 'http://117.78.7.31:7001'
//const baseUrl = 'http://168.1.111:3303'
module.exports = {
  baseUrl: baseUrl,
  /**
   * get 请求
   */
  get(url, data) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: baseUrl + url,
        data: data,
        header: {
          // 'Content-Type': 'application/json'
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          if (res.data.code == 1) {
            resolve(res.data);
          } else {
            reject(res.data);
          }
        },
        fail: function (res) {
          reject(res.data);
        }
      })
    })
  },

  /**
   * psot 请求
   */
  post(url, data) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: baseUrl + url,
        data: data,
        method: 'POST',
        header: {
          'Cache-Control': 'no-cache',
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          console.log(res);
          if (res.data.code == 1) {
            resolve(res.data);
          } else {
            reject(res.data);
          }
        },
        fail: function (res) {
          reject(res.data);
        }
      })
    })
  },

  //url拼接
  json2Form(json) {
    var str = []
    for (var p in json) {
      str.push(encodeURIComponent(p) + "=" + encodeURIComponent(json[p]));
    }
    return str.join("&");
  },

  // 图片上传
  fileUpload(filePath) {
    console.log(filePath);
    return new Promise(resolve => {
      wx.uploadFile({
        url: baseUrl + `api/Order/updateImg`,
        filePath: filePath,
        header: {
          'content-type': 'multipart/form-data'
        },
        name: 'image',
        success(res) {
          resolve(res);
        },
        fail: function (res) {
          reject(res);
        }
      })
    })
  }

}